package com.fhj.utils;

import org.json.JSONObject;

import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewFlipper;

import com.fhj.movie.customespinner.CustomerSpinnerAdapter;
import com.fhj.movie.customespinner.CustomSpinner.MySpinnerCallback;
import com.fhj.movie.interfaces.CallbackInterface;
import com.fhj.movie_fhj_01.R;

public class ViewAddHeadview {
	
	private ListView listView;
	private Context context;
	
	private ViewFlipper flipper;
	private ImageView point;
	private String[] alltype = {"全部类型","可选座影院","可购买兑换券影院","可购买团购影院"};
	private String[] orderby = {"默认排序","按地理位置排序","按评分排序","按距离排序"};
	
	private int [] viewflippers={R.drawable.tutorail_icon1,R.drawable.tutorail_icon2,R.drawable.tutorail_icon3};
	
	private int index;
	
	private CustomerSpinnerAdapter customerSpinnerAdapter;
	
	public com.fhj.movie.customespinner.CustomSpinner getSpinners() {
		return spinners;
	}
	
	private com.fhj.movie.customespinner.CustomSpinner spinners;
	
	public ViewAddHeadview(Context  context,ListView  listView){
		this.listView = listView;
		this.context = context;
	}

	/**
	 * 添加分类HeadView
	 */
	
	spinnerCallback  spinnerCallback = null;
	
	public void setSpinnerCallback(spinnerCallback spinnerCallback) {
		this.spinnerCallback = spinnerCallback;
	}

	addViewCallback  callback = null;
	

	public void setCallback(addViewCallback callback) {
		this.callback = callback;
	}

	public void addTypeHeadView(int cityId) {
		View typeView = LayoutInflater.from(context).inflate(R.layout.movie_listview_headview_second, null);
		
		listView.addHeaderView(typeView);
		
		spinners = (com.fhj.movie.customespinner.CustomSpinner)typeView.findViewById(R.id.movie_alllocation);
//		OptionAdapter adapter = new OptionAdapter(alllocation);
//		spinners.setAdapter(adapter);
		
		Spinner spinner= (Spinner)typeView.findViewById(R.id.movie_alltype);
		OptionAdapter adapter =  new OptionAdapter(alltype);
		spinner.setAdapter(adapter);
		
		spinner=(Spinner)typeView.findViewById(R.id.movie_orderby);
		adapter =new OptionAdapter(orderby);
		spinner.setAdapter(adapter);
			
		spinners = (com.fhj.movie.customespinner.CustomSpinner) typeView.findViewById(R.id.movie_alllocation);
		updateSpinner(cityId);
		customerSpinnerAdapter = new CustomerSpinnerAdapter();
		customerSpinnerAdapter.init(context, "全部地区");
		spinners.setAdapter(customerSpinnerAdapter);
		
		spinners.setonClick(new MySpinnerCallback() {
			
			

			@Override
			public void returndata(String name, long id) {
				// TODO 自动生成的方法存根
				Toast.makeText(context, ""+name+","+id, Toast.LENGTH_SHORT).show();
				customerSpinnerAdapter.init(context, name);
				customerSpinnerAdapter.notifyDataSetChanged();
				//在此处重新处理下载的数据进行显示
				callback.addCallback(id);//回调数据进行更新
				//==================
			}
		});
			
	}

	/**
	 * @param cityId
	 */
	public void updateSpinner(int cityId) {
		HttpConnectionUtils.getCityDetailsData(context, cityId,new CallbackInterface() {

			@Override
			public void JsonCallback(JSONObject object) {
				spinners.setAllInfoEntity(JsonUtils.parseCityAllInfo(object));
			}
		});
	}

	/**
	 * 添加广告HeadView
	 */
	public void addADHeadView() {
		final View  adHeadView = LayoutInflater.from(context).inflate(R.layout.movie_listview_headview_third, null);
		listView.addHeaderView(adHeadView);
		
		flipper = (ViewFlipper)adHeadView.findViewById(R.id.movie_viewFlipper);
		flipper.setInAnimation(context, R.anim.slide_rightin);//添加滑入动画
		flipper.setOutAnimation(context,R.anim.slide_leftout);//添加滑出动画
		
		ImageView clean= (ImageView)adHeadView.findViewById(R.id.movie_clean);
		clean.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO 自动生成的方法存根
				listView.removeHeaderView(adHeadView);
			}
		});
		
		
		//ViewFlipper的第一张图片
		ImageView imageView = (ImageView)adHeadView.findViewById(R.id.movie_listview_headview_image1);
		imageView.setBackgroundResource(R.drawable.sale);
		//ViewFlipper的第二张图片
		imageView = (ImageView)adHeadView.findViewById(R.id.movie_listview_headview_image2);
		imageView.setBackgroundResource(R.drawable.sale);
		//ViewFlipper的第三张图片
		imageView = (ImageView)adHeadView.findViewById(R.id.movie_listview_headview_image3);
		imageView.setBackgroundResource(R.drawable.sale);
		
		
		Animation animation = flipper.getInAnimation();
		animation.setAnimationListener(new AnimationListener() {
			
			@Override
			public void onAnimationStart(Animation animation) {
			}
			@Override
			public void onAnimationRepeat(Animation animation) {
			}
			@Override
			public void onAnimationEnd(Animation animation) {
				point=(ImageView)adHeadView.findViewById(R.id.movie_point);
				point.setBackgroundResource(viewflippers[(++index)%3]);
			}
		});
	}

	/**
	 * 添加搜索HeadView
	 * @return 
	 */
	public void addSearchHeadView() {
	
		View  searchView = LayoutInflater.from(context).inflate(R.layout.movie_listview_headview_four, null);
		listView.addHeaderView(searchView);
		
	}
	
	/**
	 * 影院分类选项Adapter
	 * @author jiayujie
	 *
	 */
	private class OptionAdapter  extends BaseAdapter{

		private String [] s;
		public OptionAdapter(String []  s){
			this.s=s;
		}
		@Override
		public int getCount() {
			return s.length;
		}

		@Override
		public Object getItem(int position) {
			return s[position];
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			
			TextView textView=null;
			if (convertView==null) {
				textView=new TextView(context);
				convertView=textView;
				textView.setTextColor(Color.BLACK);
				textView.setGravity(Gravity.CENTER);
				textView.setPadding(8, 8, 8, 8);
			}else {
				textView=(TextView) convertView;
			}
			textView.setText(s[position]);
			return textView;
		}
	}
	
	
	public interface  addViewCallback{
		public void addCallback(long id);
	}
	
	public interface  spinnerCallback{
		public void spinnerCallBack(int id,int type);
	}
}
